package com.open.capacity.reptile.service.impl;

import com.open.capacity.reptile.entity.SourcePurchaseNotice;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reptile.dao.SourcePurchaseNoticeDao;
import com.open.capacity.reptile.service.SourcePurchaseNoticeService;


@Service
public class SourcePurchaseNoticeServiceImpl implements SourcePurchaseNoticeService {

    @Autowired
    private SourcePurchaseNoticeDao sourcePurchaseNoticeDao;

    /**
     * 添加
     *
     * @param list
     */
    public int save(List<SourcePurchaseNotice> list) {
        int count = 0;
        if (CollectionUtils.isNotEmpty(list)) {
            for (SourcePurchaseNotice item : list) {
                SourcePurchaseNotice orig = findById(item.getId());
                if (orig == null) {
                    count += sourcePurchaseNoticeDao.save(item);
                } else {
                    String aa = "";
                }
            }
        }
        return count;
    }

    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return sourcePurchaseNoticeDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SourcePurchaseNotice findById(String id) {
        return sourcePurchaseNoticeDao.findById(id);
    }

    @Override
    public String findLastDate() {
        return sourcePurchaseNoticeDao.findLastDate();
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SourcePurchaseNotice> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<SourcePurchaseNotice> list = sourcePurchaseNoticeDao.findAll(params);
        PageInfo<SourcePurchaseNotice> pageInfo = new PageInfo(list);

        return PageResult.<SourcePurchaseNotice>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SourcePurchaseNotice> findAll(Map<String, Object> params) {
        return sourcePurchaseNoticeDao.findAll(params);
    }

}
